
package engine.logic.login
{
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.FrameLabel;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	
	import newx.StageInfo;
	import newx.com.loader.SwfLoader;
	import newx.utils.debug.IO;

	public class LoginAlert
	{
		private var _container:DisplayObjectContainer;
		
		static  private var _loginAlert	:MovieClip;
		private var _yesBtn		:SimpleButton;
		private var _loadingBtn	:MovieClip;
		private var _content		:MovieClip;
		private var _errText		:TextField;
		private var _bg			:MovieClip;
		
		private var _laterMsg:String;
		
		public function LoginAlert()
		{
			if(	null	!=	_self)
				throw new Error("LoginAlert 单例错误");
		}
		
		static private var _self:LoginAlert;
		
		
		
//		private function initLoginAlert():void
//		{
////			var loader:Loader=new Loader();
////			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,alertResCompleteHandler);
////			loader.load(new URLRequest("res/swf/LoginAlert.swf"));
//			SwfLoader.StartLoad("res/swf/LoginAlert.swf",null,alertResCompleteHandler);
//		}
		
//		private function alertResCompleteHandler(val:DisplayObject,pList:Array):void
//		{
////			var mc:MovieClip=LoaderInfo(e.target).content as MovieClip;
//			
//			initialize( MovieClip(val).loginAlert );
//			
//			if(""	!=	_laterMsg)
//					_self.show(_laterMsg);
//		}
		
		
		private  function initialize(mc:MovieClip):void
		{
			_container=StageInfo.uiSpace;
			
			if(_loginAlert)	
			{
				IO.traceLog("LoginAlert 只能初始化一次");
				return;
			}
			_loginAlert 	=	mc;
			_content 		=	mc.content;
			_errText		=	mc.errText;
			_content	.visible = false;
			_errText	.visible = false;
			_yesBtn			=	mc.yesBtn;
			_loadingBtn		=	mc.loadingBtn;
			_bg				=	mc.loginAlertBg;
			_bg.width		=	8000
			_bg.height		=	5000
			_bg.x			=	-3500;
			_bg.y			=	-2200;
			if(_loginAlert.stage)	_loginAlert.parent.removeChild(_loginAlert);
		}
		static public function init(mc:MovieClip):void
		{
			if(	null	==	_self )
				_self	=	new LoginAlert();
			
			_self.initialize(mc);
		}
		
		static public function showAlert(str:String,closeable:Boolean	=true):void
		{
			if(	null	==	_self )
						_self	=	new LoginAlert();
			
			if(!_loginAlert)
			{
				SwfLoader.StartLoad(ResPath.SWF+"LoginAlert.swf",null,alertResCompleteHandler,[str,closeable]);
				return;
			}
			_self.show(str,closeable);
		}
		
	    static	private function alertResCompleteHandler(val:DisplayObject,pList:Array):void
		{
			LoginAlert.init(MovieClip(val).loginAlert);
			_self.show(pList[0],pList[1]);
		}
		
		static public function hide():void
		{
			if(null		!=	_self)
					_self.remove();
		}
		
		static public function recycle():void
		{
			if(	null	!=	_self)
			{
				_self.remove();
				_self	=	null;
			}
		}
		
		private function remove():void
		{
			_laterMsg="";
			
			if((null	!=	_loginAlert)	&&	(null	!=	_loginAlert.parent))
							_loginAlert.parent.removeChild(_loginAlert);
		}
		
		private  function show(str:String,closeable:Boolean):void
		{
			
			if(null	==_loginAlert)
			{
//				_laterMsg=str;
//				initLoginAlert();
				return;
			}
			

			if(false	==	closeable)
			{
				_yesBtn.visible		=	false;
				_loadingBtn.visible	=	true;
//				_content.visible	=	false;
			}
			else
			{
				_yesBtn.visible		=	true;
				_loadingBtn.visible	=	false;
//				_content.visible	=	true;
			}
			var label:String = str.indexOf("UIStr")!=-1?str:"UIStr_"+str
			var labArr:Array = _content.currentLabels;
			var filterArr:Array = labArr.filter(
				function (item:FrameLabel,index:int,arr:Array):Boolean
				{
					return item.name == label;
				});
			if(filterArr.length > 0)
			{
				_content.gotoAndStop(label);
				_content.visible = true;
				_errText.visible = false;
			}else
			{
				_content.visible = false;
				_errText.visible = true;
				_errText.text = "ErrorCode:"+str;
			}
			
			_container.addChild(_loginAlert);
			_yesBtn.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		private function clickHandler(e:MouseEvent):void
		{
			_loginAlert.parent.removeChild(_loginAlert);
			
			
			if(	LoginLogic.LoginMode	==	LoginLogic.Mode_UniteLogin)
			{
				var url:String	=	ModelLocator.getInstance().mainIni.getContent("loginConfig","homesite");
				navigateToURL( new URLRequest(url),"_self");
			}
			_yesBtn.removeEventListener(MouseEvent.CLICK,clickHandler);
		}
	}
}